﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using BLL;
using BE;

namespace Language_Editor
{
    public partial class LanguageEditor : RadForm
    {
        private StringPlaceHolder AdminOn;
        private StringPlaceHolder AdminOff;
        int currentState;
        private const int NORMAL_STATE = 1; //showing only default language
        private const int ENHANCED_STATE = 2; //showing all texts in default and quicktranslate language
        private const int TEXT_CHANGES_STATE = 3; //showing all the tags that had their original value changed and need an update
        public LanguageEditor()
        {
            AdminOff = new StringPlaceHolder();
            AdminOn = new StringPlaceHolder();
            InitializeComponent();
            AdminOff.Text = "Du har bruger privilegier";
            AdminOff.Name = "AdminOff";
            AdminOn.Text = "Du har administrator privilegier";
            AdminOn.Name = "AdminOn";
            Admin = false;
            LblCurrentValue.Text = ConfigManager.Config.AppSettings.Settings[ConfigManager.LANGUAGE].Value;
            currentState = NORMAL_STATE;
            updateLanguageList();

        }
        private void AddAdmin()
        {
            
        }

        private void RemoveAdmin()
        {
            
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            PickLanguage plw = new PickLanguage();
            CultureInfo ci = plw.getNew();
            if (ci != null)
            {
                ConfigManager.Config.AppSettings.Settings[ConfigManager.LANGUAGE].Value = ci.Name;
                ConfigManager.Config.Save();
                
                LblCurrentValue.Text = ci.Name;
            }
        }
        
        private void updateLanguageList()
        {
            string currentLanguage = ConfigManager.Config.AppSettings.Settings[ConfigManager.LANGUAGE].Value;
            string PreferedLanguage = ConfigManager.Config.AppSettings.Settings[ConfigManager.PREFEREDLANGUAGE].Value;
            IEnumerable<Language> datList=null;
            switch(currentState)
            {
                case NORMAL_STATE:
                    datList = languageConnector.getAllLanguages(currentLanguage);
                    break;
                case ENHANCED_STATE:
                    datList = languageConnector.getAllLanguages(currentLanguage,PreferedLanguage);
                    break;
                case TEXT_CHANGES_STATE:
                    datList = languageConnector.getChangedLanguages();
                    break;
                default:
                    datList = languageConnector.getAllLanguages(currentLanguage);
                    break;
            }
            LanguageTable.DataSource = datList;
        }
        private bool admin;
        public bool Admin
        {
            get
            {
                return admin;
            }
            set
            {
                if (value)
                    LblStatusText.Text = AdminOn.ToString();
                else
                    LblStatusText.Text = AdminOff.ToString();
                admin = value;
            }
        }

        private void MenuToggleAdmin_Click(object sender, EventArgs e)
        {
            if (admin){
                Admin = false;
                RemoveAdmin();
            }
            else
            {
                TextboxWindow window = new TextboxWindow("Skriv adgangskoden for Administrator: ");
                if (window.getText().Equals("Vetech@EsbjergV2013"))
                {
                    Admin = true;
                    AddAdmin();
                }
            }
        }

        private void LanguageTable_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
        {
            RadMenuSeparatorItem separator = new RadMenuSeparatorItem();
            e.ContextMenu.Items.Add(separator);

            RadMenuItem QuickTranslate = new RadMenuItem("Hurtig Oversæt");
            QuickTranslate.AutoToolTip = true;
            QuickTranslate.ToolTipText = "Oversæt dette element til det foretrukne sprog valgt i indstillinger";
            QuickTranslate.Click += new EventHandler(QuickTranslateThis);
            e.ContextMenu.Items.Add(QuickTranslate);

            RadMenuItem QuickEdit = new RadMenuItem("rediger tekst");
            QuickEdit.Click += new EventHandler(QuickEditThis);
            e.ContextMenu.Items.Add(QuickEdit);

            if (Admin) { 
             RadMenuItem InsertChoise = new RadMenuItem("Insert new element");
            InsertChoise.Click += new EventHandler(insertNewElement);
            e.ContextMenu.Items.Add(InsertChoise);
            }
        }
        private void QuickTranslateThis(Object sender, EventArgs e)
        {
            string dcultureName = ConfigManager.Config.AppSettings.Settings[ConfigManager.PREFEREDLANGUAGE].Value;
            CultureInfo preferedCulture = new CultureInfo(dcultureName);
            Language language = (Language)LanguageTable.SelectedRows[0].DataBoundItem;
            TextboxWindow window = new TextboxWindow("Oversæt teksten \""+language.Text+"\" til "+preferedCulture.DisplayName);
            string newText = window.getText();
            
            if (newText != null && newText.Length > 0)
            {
                Language newLanguage = new Language();
                newLanguage.Text = newText;
                newLanguage.LanguageCode = dcultureName;
                newLanguage.MasterControl = language.MasterControl;
                newLanguage.TagId = language.TagId;
                languageConnector.addLanguage(newLanguage);
            }
            updateLanguageList();
        }
        private void QuickEditThis(Object sender, EventArgs e)
        {
            Language language = (Language)LanguageTable.SelectedRows[0].DataBoundItem;
            if (languageConnector.getDefaultLanguage().Name.Equals(language.LanguageCode))
            {
                if (Admin)
                    QuickEdit(language);
                else
                    MessageBox.Show("Du kan ikke ændre denne tekst da det er originalen og du ikke er administrator", "Handling nægtet", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            else
            {
                QuickEdit(language);
            }
            
        }

        private void QuickEdit(Language language)
        {
            TextboxWindow window = new TextboxWindow("Rediger teksten: "+language.Text);
            string newText = window.getText();
            if (newText != null && newText.Length > 0)
            {
                language.Text = newText;
                languageConnector.edit(language);
            }
            updateLanguageList();
        }
        private void insertNewElement(Object sender, EventArgs e)
        {
            if (Admin)
            {
                NewElement ne = new NewElement();
                ne.ShowDialog();
                updateLanguageList();
            }
        }

        private void MenuSettings_Click(object sender, EventArgs e)
        {
            Settings settings = new Settings();
            settings.ShowDialog();
        }

        private void MenuShow2Languages_Click(object sender, EventArgs e)
        {
            currentState = ENHANCED_STATE;
            updateLanguageList();
        }

        private void MenuTextChanges_Click(object sender, EventArgs e)
        {
            currentState = TEXT_CHANGES_STATE;
            updateLanguageList();
        }

        private void MenuDoneWithTranslations_Click(object sender, EventArgs e)
        {
            currentState = NORMAL_STATE;
            languageConnector.TextsChanged();
            updateLanguageList();
        }

        private void MenuShowNormal_Click(object sender, EventArgs e)
        {
            currentState = NORMAL_STATE;
            updateLanguageList();
        }





    }
}
